﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SmartMD.DataModel.Entity;
using System.Collections.ObjectModel;
using SmartMD.DataAccess.Service;
using Techstar.Dialog;
using SmartMD.DataModel;
using SmartMD.Manager.Common;
using SmartMD.DataAccess.Interface.Dao;

namespace SmartMD.Manager.SysInfo
{
    /// <summary>
    /// Interaction logic for ExamItemDefinition.xaml
    /// </summary>
    public partial class ExamItemMaintain : BaseWindow
    { 
        private List<ExamItemInfo> examItemList = null;
        //维护的核心对象
        private ExamItemInfo _dataSource;
        public ExamItemInfo DataSource
        {
            get
            {
                return _dataSource;
            }
            set
            {
                _dataSource = value;
                this.ucItemList.AddItem(_dataSource);
                this.ucExamItemDefine.DataSource = _dataSource;
            }
        }

        public ExamItemMaintain()
        {
            InitializeComponent();
            this.RegisterToolbar(this.tbBasicDataTool);
        
            this.Title = "检查项目维护";
            this.ucItemList.Owner = this;
           
         
        }

        protected override void OnInitialized(EventArgs e)
        {
            ExamItemListPad.Instance.Owner = this;
            base.OnInitialized(e);
           
        }

        public static string ModuleName 
        {
            get{return "检查项目维护";}
        }

        private void btnNewItem_Click(object sender, RoutedEventArgs e)
        {
            NewExamItemDialog dlg = new NewExamItemDialog(this);
            dlg.ShowDialog();
            this.ucExamItemDefine.IsEnabled = true;
        }
       
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Commit();
        }

        public void Commit()
        {
            if (this.ucItemList.examItemInfos == null)
                return;
            var items = new ObservableCollection<ExamItemInfo>();
            foreach (var examItemInfo in this.ucItemList.examItemInfos)
            {
                if (examItemInfo.State != RecordState.Normal)
                    items.Add(examItemInfo);
            }
            if (items.Count == 0)
            {
                TaskDialogEx.Warning("没有可以提交到数据服务器的内容!");
                return;
            }

            /* string errMessage;
           if (!MainWindow.Instance.TestServiceConnect(out errMessage))
            {
                System.Windows.MessageBox.Show(errMessage, @"不能提交", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            } */

            new CommitDialog(items, (IBaseGenericDao)DataServiceManager.ExamItemService, (BaseWindow)this).ShowDialog();
        }

        private void tabControlEdit_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void BtnEdit_Click(object sender, RoutedEventArgs e)
        {
            this.ucItemList.EditSelectedItem();
            this.ucExamItemDefine.IsEnabled = true;
            this.ucExamItemDefine.cbIsEnabled.IsEnabled = true;
        }

        private void BtnDelete_Click(object sender, RoutedEventArgs e)
        {
            this.ucItemList.DeleteSelectedItem();
        }

        public override void DeleteEntity(Object entity)
        {
            this.ucItemList.DeleteSelectedItem((ExamItemInfo)entity);
        }
    }
}
